define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Aftalk, $stateParams, GoPage, GoDialog, GoPicture) {

		$scope.order_id = $stateParams.order_id;
		$scope.is_new = $stateParams.is_new;
		$scope.act_type = $stateParams.act_type;
		$scope.act_id = $stateParams.act_id;
		$scope.product_id = $stateParams.product_id;
		$scope.product_id = '0';
		$scope.comments = new Array();
		$scope.commentArr = 0;
		$scope.goods_id = new Array();
		$scope.star1 = new Object();
		$scope.star2 = new Object();
		$scope.star3 = new Object();
		$scope.score = new Array();
		$scope.sendGoods = new Array();
		$scope.is_hidden = new Array();
		$scope.is_new = new Array();

		$scope.input = new Object();
		$scope.input.evaluation = new Array();	//用于显示初评评分
		$scope.input.uploads = new Array(); //存放图片id，要提交的
		$scope.input.uploads_show = new Array();//存放图片url，显示用
		$scope.input.comment_id = new Array();
		//$scope.input.is_new = true;		//用于判断是否为追加

		//数据初始化。evaluation：1差评 3中评 5好评
		$scope.init_data = function() {
			Aftalk.get_order($scope.order_id, function(data) {
				$scope.orders = data;
				console.log(data)
				if(!$scope.orders){
					return;
				}
				$scope.commentArr = data.goods.length;
				for(var i = 0; i < $scope.commentArr; i++) {
					$scope.goods_id[i] = data.goods[i].goods_id;
					if(data.comments_count == 0 || data.comments_count == null) {
						$scope.is_new[i] = true;
						$scope.input.evaluation[i] = 5;//默认5  好评
						$scope.input.comment_id[i] = 0;
						$scope.is_hidden[i] = false;
					} else {
						$scope.is_new[i] = false;
						$scope.comments[i]=new Object();
						$scope.comments[i]=data.goods[i].discuss_info[0].comment;
						$scope.is_hidden[i]=new Object();
						$scope.is_hidden[i]=data.goods[i].discuss_info[0].is_hidden;
						$scope.input.evaluation[i] = data.goods[i].discuss_info[0].evaluation;
						$scope.input.comment_id[i] = data.goods[i].discuss_info[0].comment_id;
						$scope.score = data.goods[i].discuss_info[0].score;
						$scope.goods_id[i] = data.goods[i].goods_id;
					}
				}
			});
		}
		$scope.init_data(); //必须在定义之后

		//获取星数
		$scope.starNum_desc = 5;
		$scope.starNum_service = 5;
		$scope.starNum_send = 5;
		
		/*==========图片上传、删除=============*/
		$scope.doShowFile = function() {
			document.getElementById("aftalk-upfilebutton").click();
		}
		//图片上传
		$scope.addStoreImg = function(index) {
				if(!($scope.input.uploads[index] instanceof Array)) { //让每个元素都为数组
					$scope.input.uploads[index] = new Array();
				}
				if(!($scope.input.uploads_show[index] instanceof Array)) { //让每个元素都为数组
					$scope.input.uploads_show[index] = new Array();
				}
				if($scope.input.uploads[index].length>=4){
					GoDialog.toast("最多只能晒图4张！");
					return;
				}
				GoPicture.upload(function(res) {
					if(!res.success) {
						GoDialog.toast(res.msg);
						return;
					}
					$scope.input.uploads_show[index].push(res.result.image_src);//显示用
					$scope.input.uploads[index].push(res.result.image_id);
				});
			}
		
			//图片放大
			$scope.showmaximg=function(imgUrl){
				GoDialog.showBigImg(imgUrl);
			}
			//图片删除
			$scope.delimg=function(parent_index, index){
				$scope.input.uploads[parent_index].splice(index, 1);
				$scope.input.uploads_show[parent_index].splice(index, 1);
			}
		/*==========图片上传、删除=============*/
		
		//得到score和evaluation
		$scope.getscore = function() {
			$scope.score.splice(0,$scope.score.length);//必须先清空，否则可能重复
			$scope.sendGoods.splice(0,$scope.sendGoods.length);//必须先清空，否则可能重复
//			if($scope.input.is_new){
				$scope.star1.name = "产品描述";
				$scope.star2.name = "服务态度";
				$scope.star3.name = "发货速度";
				$scope.star1.point = $scope.starNum_desc;
				$scope.star2.point = $scope.starNum_service;
				$scope.star3.point = $scope.starNum_send;
				$scope.score.push($scope.star1);
				$scope.score.push($scope.star2);
				$scope.score.push($scope.star3);
//			}
			for(var i = 0; i < $scope.commentArr; i++) {
				$scope.goods = new Object();
				$scope.goods.goods_id = $scope.goods_id[i];
				$scope.goods.comments = $scope.comments[i];
				$scope.goods.is_new = $scope.is_new[i];
				$scope.goods.is_hidden = $scope.is_hidden[i];
				$scope.goods.evaluation = $scope.input.evaluation[i];
				$scope.goods.comment_id = $scope.input.comment_id[i];
				$scope.goods.image = $scope.input.uploads[i];
				$scope.sendGoods.push($scope.goods);
			}
		}

		//发布评论
		$scope.sendComment = function() {			
			$scope.getscore();
			Aftalk.send_comment($scope.order_id, $scope.sendGoods, $scope.score, function(data) {
				if(data.success) {
					GoDialog.toast("发表成功！");
					GoPage.go_back();
				} else {
					GoDialog.toast(data.msg);
				}
			});
		}
		
		//页面跳转
		$scope.onshow_first = function(store_id){
			GoPage.go_states("first",{"index":0,"store_id":store_id,"coupon_id":""});
		}
		
		$scope.onshow_detail = function(goods_id,product_id) {
			if($scope.act_type == 'normal') {
				$scope.act_type = 'goods';
			}
			if($scope.act_type == 'newtest' )
			GoPage.go_states("newtest_detail",{"act_id":$scope.act_id,"goods_id":goods_id});
			else if($scope.act_type == 'newnotice')
			GoPage.go_states("newnotice_detail",{"act_id":$scope.act_id,"goods_id":goods_id});
			else if($scope.act_type == 'newsend')
			GoPage.go_states("newsend_detail",{"act_id":$scope.act_id,"goods_id":goods_id});
			else if($scope.act_type == 'groupbuy')
			GoPage.go_states("groupbuy_goods",{"act_id":$scope.act_id,"goods_id":goods_id,"product_id":product_id});
			else if($scope.act_type == 'timedbuy')
			GoPage.go_states("panicbuying_goods",{"act_id":$scope.act_id,"goods_id":goods_id,"product_id":product_id});
			else if($scope.act_type == 'spike')
			GoPage.go_states("seckill_goods",{"act_id":$scope.act_id,"goods_id":goods_id,"product_id":product_id});
			else
			GoPage.go_states("productdetail", { "goods_id": goods_id, 'type': $scope.act_type });
		};
		//是否选择匿名
		$scope.handleHide = function(index){
			$scope.is_hidden[index] = !$scope.is_hidden[index];
		}
		//选择的评价
		$scope.is_showevaluate=function(id,index){
		    if(id==1){
		    	$scope.input.evaluation[index]=5;
		    }else if(id==2){
		   		$scope.input.evaluation[index]=3;
		    }else{
		    	$scope.input.evaluation[index]=1;
		    }
		}
		
		//选择的星数
		//存亮星星图片
		$scope.star_desc = new Array();
		$scope.star_service = new Array();
		$scope.star_send = new Array();
		
		$scope.setstars=function(objce,index){
			for(var i=0;i<5;i++){
				objce[i]="starimgno";
			}
			for(var i=0;i<index;i++){
				objce[i]="starimg";
			}			
			return objce;
		}
			$scope.star_desc=$scope.setstars($scope.star_desc,5);
			$scope.star_service=$scope.setstars($scope.star_service,5);
			$scope.star_send=$scope.setstars($scope.star_send,5);
		$scope.getdesc=function(type,index){
			if(type==1){
				$scope.star_desc=$scope.setstars($scope.star_desc,index);
				$scope.starNum_desc = index;				
			}else if(type==2){
				$scope.star_service=$scope.setstars($scope.star_service,index);
				$scope.starNum_service = index;
			}else{
				$scope.star_send=$scope.setstars($scope.star_send,index);
				$scope.starNum_send = index;
			}
		}


	}


	controller.$inject = ['$scope', 'Aftalk', '$stateParams', 'GoPage', 'GoDialog', 'GoPicture'];

	return app.registerController("aftalkctrl", controller);
});